package org.xfy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xfy.org.query.EmployeeQuery;
import org.xfy.org.domain.Employee;
import org.xfy.org.service.IEmployeeService;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "人员的API", description = "人员相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping("/echarts")
    public AjaxResult echarts(){
        return employeeService.echarts();
    }
    @PostMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult save(@RequestBody Employee employee) {
        if (employee.getId() == null) {//新增
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult del(@PathVariable("id") Long id) {
        employeeService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public AjaxResult PatchDel(@RequestBody Long[] ids) {
        employeeService.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        Employee employee = employeeService.findOne(id);
        return AjaxResult.me().setResultObj(employee);
    }

    @GetMapping()
    @ApiOperation(value = "查询所有")
    public AjaxResult findAll() {
        List<Employee> employees = employeeService.findAll();
        return AjaxResult.me().setResultObj(employees);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody EmployeeQuery query) {
        PageList<Employee> pageList = employeeService.queryPage(query);
        return AjaxResult.me().setResultObj(pageList);
    }
}
